﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Forms;
using Microsoft.WindowsMobile.PocketOutlook;

namespace Senthil.Mobile.WhereAmI
{
    public partial class AutoSMSConfigureForm : Form
    {
        string[] users;
        bool autoSMSEnabled;
        string messageText;

        public AutoSMSConfigureForm()
        {
            InitializeComponent();
        }

        public AutoSMSConfigureForm(bool autoSMSEnabled, string messageText, string[] users) : this()
        {
            this.autoSMSEnabled = autoSMSEnabled;
            this.messageText = messageText;
            this.users = users;
        }

        private void changeContactsLabel_Click(object sender, EventArgs e)
        {
            string user = string.Empty;

            using (ChooseContactDialog contactsDialog = new ChooseContactDialog())
            {
                contactsDialog.RequiredProperties = new ContactProperty[] { ContactProperty.MobileTelephoneNumber };

                if (contactsDialog.ShowDialog() == DialogResult.OK)
                {
                    user = contactsDialog.SelectedContact.MobileTelephoneNumber;

                    if (users.Contains(user))
                    {
                        MessageBox.Show(string.Format("{0} is already in the selected list of contacts.", user), "Cannot add user");
                        return;
                    }

                    contactsListTextBox.Text = contactsListTextBox.Text + user + Environment.NewLine;
                }
            }
        }

        private void okMenuItem_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        public string[] Users
        {
            get { return contactsListTextBox.Text.Split('\r', '\n')
                .Where((x) => !string.IsNullOrEmpty(x))
                .ToArray(); 
            }
        }

        public string ConfiguredMessage
        {
            get { return configuredMessageTextBox.Text; }
        }

        public bool AutoSMSReplyEnabled
        {
            get { return enableCheckBox.Checked; }
        }

        private void cancelMenuItem_Click(object sender, EventArgs e)
        {   
            this.DialogResult = DialogResult.Cancel;
        }

        private void AutoSMSConfigureForm_Load(object sender, EventArgs e)
        {
            this.enableCheckBox.Checked = autoSMSEnabled;
            this.configuredMessageTextBox.Text = this.messageText;

            foreach (string user in users)
            {
                contactsListTextBox.Text += user + Environment.NewLine;
            }

            enableCheckBox.Focus();
        }
    }
}